home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / rtfgenerator.cpp < prev    next >
C/C++ Source or Header  |  2005-03-20  |  8KB  |  274 lines

  1. /***************************************************************************
  2.                           rtfcode.cpp  -  description
  3.                              -------------------
  4.     begin                : Die Jul 9 2002
  5.     copyright            : (C) 2002 by AndrΘ Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "rtfgenerator.h"
  19.  
  20. using namespace std;
  21.  
  22. namespace highlight {
  23.  
  24. string RtfGenerator::formatStyleAttributes( const ElementStyle & col)
  25. {
  26.   stringstream s;
  27.   s  << "\\red"<< col.getColour().getRTFRedValue()
  28.      << "\\green"<<col.getColour().getRTFGreenValue()
  29.      << "\\blue"<<col.getColour().getRTFBlueValue()
  30.      << ";";
  31.  
  32.   return s.str();
  33. }
  34.  
  35. string  RtfGenerator::getOpenTag(int styleNumber,const ElementStyle & elem)
  36. {
  37.   ostringstream s;
  38.   s << "{\\cf"<<(styleNumber+1)<<"{";
  39.   if (elem.isBold()) s << "\\b ";
  40.   if (elem.isItalic()) s << "\\i ";
  41.   if (elem.isUnderline()) s << "\\ul ";
  42.   return  s.str();
  43. }
  44.  
  45. string  RtfGenerator::getCloseTag(const ElementStyle &elem)
  46. {
  47.   ostringstream s;
  48.   if (elem.isBold()) s << "\\b0 ";
  49.   if (elem.isItalic()) s << "\\i0 ";
  50.   if (elem.isUnderline()) s << "\\ul0 ";
  51.   s << "}}";
  52.   return  s.str();
  53. }
  54.  
  55. RtfGenerator::RtfGenerator(const string &colourTheme)
  56.     : CodeGenerator( colourTheme)
  57. {
  58.   styleTagOpen.push_back(getOpenTag(STANDARD, docStyle.getDefaultStyle()));
  59.   styleTagOpen.push_back(getOpenTag(STRING, docStyle.getStringStyle()));
  60.   styleTagOpen.push_back(getOpenTag(NUMBER, docStyle.getNumberStyle()));
  61.   styleTagOpen.push_back(getOpenTag(SL_COMMENT, docStyle.getSingleLineCommentStyle()));
  62.   styleTagOpen.push_back(getOpenTag(ML_COMMENT_BEGIN,docStyle.getCommentStyle()));
  63.   styleTagOpen.push_back(getOpenTag(ESC_CHAR, docStyle.getEscapeCharStyle()));
  64.   styleTagOpen.push_back(getOpenTag(DIRECTIVE_LINE, docStyle.getDirectiveStyle()));
  65.   styleTagOpen.push_back(getOpenTag(DIRECTIVE_STRING, docStyle.getDirectiveStringStyle()));
  66.   styleTagOpen.push_back(getOpenTag(LINENUMBER, docStyle.getLineStyle()));
  67.   styleTagOpen.push_back(getOpenTag(SYMBOL, docStyle.getSymbolStyle()));
  68.  
  69.   styleTagClose.push_back(getCloseTag(docStyle.getDefaultStyle()));
  70.   styleTagClose.push_back(getCloseTag(docStyle.getStringStyle()));
  71.   styleTagClose.push_back(getCloseTag(docStyle.getNumberStyle()));
  72.   styleTagClose.push_back(getCloseTag(docStyle.getSingleLineCommentStyle()));
  73.   styleTagClose.push_back(getCloseTag(docStyle.getCommentStyle()));
  74.   styleTagClose.push_back(getCloseTag(docStyle.getEscapeCharStyle()));
  75.   styleTagClose.push_back(getCloseTag(docStyle.getDirectiveStyle()));
  76.   styleTagClose.push_back(getCloseTag(docStyle.getDirectiveStringStyle()));
  77.   styleTagClose.push_back(getCloseTag(docStyle.getLineStyle()));
  78.   styleTagClose.push_back(getCloseTag(docStyle.getSymbolStyle()));
  79.  
  80.   newLineTag = "\\par\\pard\n";
  81.   spacer = " ";
  82. }
  83.  
  84. RtfGenerator::RtfGenerator()
  85. {}
  86. RtfGenerator::~RtfGenerator()
  87. {}
  88.  
  89. string RtfGenerator::getHeader(const string & title)
  90. {
  91.   return string();
  92. }
  93.  
  94. void RtfGenerator::printBody()
  95. {
  96.  *out << "{\\rtf1\\ansi\\uc0 \\deff1"
  97.        << "{\\fonttbl{\\f1\\fmodern\\fprq1\\fcharset0 Courier;}}"
  98.        << "{\\colortbl;";
  99.  
  100.   *out << formatStyleAttributes(docStyle.getDefaultStyle());
  101.  
  102.   *out << formatStyleAttributes(docStyle.getStringStyle());
  103.   *out << formatStyleAttributes(docStyle.getNumberStyle());
  104.   *out << formatStyleAttributes(docStyle.getSingleLineCommentStyle());
  105.  
  106.   *out << formatStyleAttributes(docStyle.getCommentStyle());
  107.   *out << formatStyleAttributes(docStyle.getEscapeCharStyle());
  108.   *out << formatStyleAttributes(docStyle.getDirectiveStyle());
  109.  
  110.   *out << formatStyleAttributes(docStyle.getDirectiveStringStyle());
  111.   *out << formatStyleAttributes(docStyle.getLineStyle());
  112.   *out << formatStyleAttributes(docStyle.getSymbolStyle());
  113.  
  114.   /* For output formats which can refer to external styles it is more safe
  115.      to use the colour theme's keyword class names, since the language
  116.      definitions (which may change during a batch conversion) do not have to define
  117.      all keyword classes, that are needed to highlight all input files correctly.
  118.      It is ok for RTF to use the language definition's class names, because RTF
  119.      does not refer to external styles.
  120.      We cannot use the theme's class names, because KSIterator returns an
  121.      alphabetically ordered list, which is not good because RTF is dependent
  122.      on the order. We access the keyword style with an ID, which is calculated
  123.      ignoring the alphabetic order.
  124.   */
  125.   vector<string>  keywordClasses = langInfo.getKeywordClasses();
  126.   for (unsigned int i=0;i<keywordClasses.size();i++){
  127.       *out << formatStyleAttributes(docStyle.getKeywordStyle(keywordClasses[i]));
  128.   }
  129.  
  130.   *out << "}\n{\\info }\\paperw11900\\paperh16820\\margl560\\margr560\\margt840"
  131.        << "\\margb840\\widowctrl\\ftnbj\\aenddoc\\formshade \\fet0\\sectd"
  132.        << "\\linex0\\endnhere\\plain\\f1\\fs20\n\\pard ";
  133.   processRootState();
  134.   *out << "}}"<<endl;
  135. }
  136.  
  137.  
  138. string RtfGenerator::getFooter()
  139. {
  140.  return string();
  141. }
  142.  
  143. /** Gibt RTF-Code der Sonderzeichen zurueck */
  144. string RtfGenerator::maskCharacter(unsigned char c)
  145. {
  146.   switch (c)
  147.     {
  148.     case '}' :
  149.     case '{' :
  150.     case '\\' :
  151.       {
  152.         string m;
  153.         m="\\";
  154.         return m+=c;
  155.       }
  156.       break;
  157.     case '0':
  158.     case '1':
  159.     case '2':
  160.     case '3':
  161.     case '4':
  162.     case '5':
  163.     case '6':
  164.     case '7':
  165.     case '8':
  166.     case '9':
  167.       {
  168.         string m;
  169.         m="{";
  170.         m+=c;
  171.         m+="}";
  172.         return m;
  173.       }
  174.       break;
  175.     case AUML_LC:
  176.       return "\\'e4";
  177.       break;
  178.     case OUML_LC:
  179.       return "\\'f6";
  180.       break;
  181.     case UUML_LC:
  182.       return "\\'fc";
  183.       break;
  184.     case AUML_UC:
  185.       return "\\'c4";
  186.       break;
  187.     case OUML_UC:
  188.       return "\\'d6";
  189.       break;
  190.     case UUML_UC:
  191.       return "\\'dc";
  192.       break;
  193.  
  194.     case AACUTE_LC:
  195.       return "\\'e1";
  196.       break;
  197.     case EACUTE_LC:
  198.       return "\\'e9";
  199.       break;
  200.     case OACUTE_LC:
  201.       return "\\'f3";
  202.       break;
  203.     case UACUTE_LC:
  204.       return "\\'fa";
  205.       break;
  206.  
  207.     case AGRAVE_LC:
  208.       return "\\'e0";
  209.       break;
  210.     case EGRAVE_LC:
  211.       return "\\'e8";
  212.       break;
  213.     case OGRAVE_LC:
  214.       return "\\'f2";
  215.       break;
  216.     case UGRAVE_LC:
  217.       return "\\'f9";
  218.       break;
  219.  
  220.     case AACUTE_UC:
  221.       return "\\'c1";
  222.       break;
  223.     case EACUTE_UC:
  224.       return "\\'c9";
  225.       break;
  226.     case OACUTE_UC:
  227.       return "\\'d3";
  228.       break;
  229.     case UACUTE_UC:
  230.       return "\\'da";
  231.       break;
  232.     case AGRAVE_UC:
  233.       return "\\'c0";
  234.       break;
  235.     case EGRAVE_UC:
  236.       return "\\'c8";
  237.       break;
  238.     case OGRAVE_UC:
  239.       return "\\'d2";
  240.       break;
  241.     case UGRAVE_UC:
  242.       return "\\'d9";
  243.       break;
  244.  
  245.     case SZLIG:
  246.       return "\\'df";
  247.       break;
  248.    // skip  first byte of multibyte chracters
  249.  /*   #ifndef _WIN32
  250.     case 195:
  251.       return string("");
  252.       break;
  253. #endif*/
  254.  
  255.     default :
  256.       {
  257.        string m;
  258.        return m += c;
  259.       }
  260.     }
  261. }
  262.  
  263. string RtfGenerator::getMatchingOpenTag(unsigned int styleID){
  264.  return getOpenTag(KEYWORD+styleID,
  265.         docStyle.getKeywordStyle(langInfo.getKeywordClasses()[styleID]));
  266. }
  267.  
  268. string RtfGenerator::getMatchingCloseTag(unsigned int styleID){
  269.   return getCloseTag(docStyle.getKeywordStyle(langInfo.getKeywordClasses()[styleID]));
  270. }
  271.  
  272.  
  273. }
  274.